home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Interfaces / CIncludes / Math64.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-11  |  7.4 KB  |  222 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Math64.h
  3.  
  4.      Contains:    64-bit integer math Interfaces.
  5.  
  6.      Version:    Technology:    Math64Lib
  7.                  Package:    Use with Universal Interfaces 2.1
  8.  
  9.      Copyright:    © 1984-1995 by Apple Computer, Inc.
  10.                  All rights reserved.
  11.  
  12.      Bugs?:        If you find a problem with this file, use the Apple Bug Reporter
  13.                  stack.  Include the file and version information (from above)
  14.                  in the problem description and send to:
  15.                      Internet:    apple.bugs@applelink.apple.com
  16.                      AppleLink:    APPLE.BUGS
  17.  
  18. */
  19.  
  20. #ifndef __MATH64__
  21. #define __MATH64__
  22.  
  23.  
  24. #ifndef __TYPES__
  25. #include <Types.h>
  26. #endif
  27. /*    #include <ConditionalMacros.h>                                */
  28.  
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32.  
  33. #if PRAGMA_ALIGN_SUPPORTED
  34. #pragma options align=mac68k
  35. #endif
  36.  
  37. #if PRAGMA_IMPORT_SUPPORTED
  38. #pragma import on
  39. #endif
  40.  
  41. /*--------------------------------------------------------------------------------
  42.                 These routines are intended to provide C software support for
  43.                 64 bit integer types.  Their behavior should mimic anticipated
  44.                 64 bit hardware. This implementation should replace use of the
  45.                 "wide" type found in PowerPC.
  46.  
  47.     The following routines are available for performing math on 64-bit integers:
  48.     
  49.     S64Max
  50.                 Returns the largest representable SInt64.
  51.     S64Min
  52.                 Returns the smallest (i.e. most negative) SInt64.  Note: the negative
  53.                 (absolute value) of this number is not representable in an SInt64.
  54.                 That means that S64Negate(S64Min) is not representable (in fact,
  55.                 it returns S64Min).
  56.     S64Add
  57.                 Adds two integers, producing an integer result.  If an overflow
  58.                 occurs the result is congruent mod (2^64) as if the operands and
  59.                 result were unsigned.  No overflow is signaled.
  60.     
  61.     S64Subtract
  62.                 Subtracts two integers, producing an integer result.  If an overflow
  63.                 occurs the result is congruent mod (2^64) as if the operands and
  64.                 result were unsigned.  No overflow is signaled.
  65.  
  66.     S64Negate
  67.                 Returns the additive inverse of a signed number (i.e. it returns
  68.                 0 - the number).  S64Negate (S64Min) is not representable (in fact,
  69.                 it returns S64Min).
  70.     
  71.     S64Absolute
  72.                 Returns the absolute value of the number (i.e. the number if
  73.                 it is positive, or 0 - the number if it is negative).
  74.                 See S64Negate above.
  75.                 
  76.     S64Multiply
  77.                 Multiplies two signed numbers, producing a signed result.  Overflow
  78.                 is ignored and the low-order part of the product is returned.  The
  79.                 sign of the result is not guaranteed to be correct if the magnitude
  80.                 of the product is not representable.
  81.     S64Divide
  82.                 Divides dividend by divisor, returning the quotient.  The remainder
  83.                 is returned in *remainder if remainder (the pointer) is non-NULL.
  84.                 The sign of the remainder is the same as the sign of the dividend
  85.                 (i.e. it takes the absolute values of the operands, does the division,
  86.                 then fixes the sign of the quotient and remainder).  If the divisor
  87.                 is zero, then S64Max() will be returned (or S64Min() if the dividend
  88.                 is negative), and the remainder will be the dividend; no error is
  89.                 reported.
  90.     
  91.     S64Set
  92.                 Given an SInt32, returns an SInt64 with the same value.  Use this
  93.                 routine instead of coding 64-bit constants (at least when the
  94.                 constant will fit in an SInt32).
  95.     
  96.     S64SetU
  97.                 Given a UInt32, returns a SInt64 with the same value.
  98.     
  99.     S64Compare
  100.                 Given two signed numbers, left and right, returns an
  101.                 SInt32 that compares with zero the same way left compares with
  102.                 right.  If you wanted to perform a comparison on 64-bit integers
  103.                 of the form:
  104.                         operand_1 <operation> operand_2
  105.                 then you could use an expression of the form:
  106.                         xxxS64Compare(operand_1,operand_2) <operation> 0
  107.                 to test for the same condition.
  108.                 
  109.                 CAUTION: DO NOT depend on the exact value returned by this routine.
  110.                 Only the sign (i.e. positive, zero, or negative) of the result is
  111.                 guaranteed.
  112.  
  113.     S64And, S64Or, S64Eor and S64Not
  114.     
  115.                 Return Boolean (1 or 0) depending on the outcome of the logical
  116.                 operation.
  117.  
  118.     S64BitwiseAnd, S64BitwiseOr, S64BitwiseEor and S64BitwiseNot
  119.     
  120.                 Return the Bitwise result.
  121.                 
  122.     S64ShiftRight and S64ShiftLeft
  123.     
  124.                 The lower 7 bits of the shift argument determines the amount of 
  125.                 shifting.  S64ShiftRight is an arithmetic shift while U64ShiftRight
  126.                 is a logical shift.
  127.  
  128.     SInt64ToLongDouble
  129.                 
  130.                 Converts SInt64 to long double.  Note all SInt64s fit exactly into 
  131.                 long doubles, thus, the binary -> decimal conversion routines
  132.                 in fp.h can be used to achieve SInt64 -> long double -> decimal
  133.                 conversions.
  134.                 
  135.     LongDoubleToSInt64
  136.     
  137.                 Converts a long double to a SInt64.  Any decimal string that fits
  138.                 into a SInt64 can be converted exactly into a long double, using the
  139.                 conversion routines found in fp.h.  Then this routine can be used
  140.                 to complete the conversion to SInt64.
  141.                 
  142.                 
  143.     
  144.     The corresponding UInt64 routines are also included.
  145.     
  146. --------------------------------------------------------------------------------*/
  147. struct SInt64 {
  148.     SInt32                            hi;
  149.     UInt32                            lo;
  150. };
  151. typedef struct SInt64 SInt64;
  152.  
  153. struct UInt64 {
  154.     UInt32                            hi;
  155.     UInt32                            lo;
  156. };
  157. typedef struct UInt64 UInt64;
  158.  
  159. #if GENERATINGPOWERPC
  160. extern SInt64 S64Max(void);
  161. extern SInt64 S64Min(void);
  162. extern SInt64 S64Add(SInt64 x, SInt64 y);
  163. extern SInt64 S64Subtract(SInt64 left, SInt64 right);
  164. extern SInt64 S64Negate(SInt64 value);
  165. extern SInt64 S64Absolute(SInt64 value);
  166. extern SInt64 S64Multiply(SInt64 xparam, SInt64 yparam);
  167. extern SInt64 S64Divide(SInt64 dividend, SInt64 divisor, SInt64 *remainder);
  168. extern SInt64 S64Set(SInt32 value);
  169. extern SInt64 S64SetU(UInt32 value);
  170. extern SInt32 S32Set(SInt64 value);
  171. extern int S64Compare(SInt64 left, SInt64 right);
  172. extern Boolean S64And(SInt64 left, SInt64 right);
  173. extern Boolean S64Or(SInt64 left, SInt64 right);
  174. extern Boolean S64Eor(SInt64 left, SInt64 right);
  175. extern Boolean S64Not(SInt64 value);
  176. extern SInt64 S64BitwiseAnd(SInt64 left, SInt64 right);
  177. extern SInt64 S64BitwiseOr(SInt64 left, SInt64 right);
  178. extern SInt64 S64BitwiseEor(SInt64 left, SInt64 right);
  179. extern SInt64 S64BitwiseNot(SInt64 value);
  180. extern SInt64 S64ShiftRight(SInt64 value, UInt32 shift);
  181. extern SInt64 S64ShiftLeft(SInt64 value, UInt32 shift);
  182. extern long double SInt64ToLongDouble(SInt64 value);
  183. extern SInt64 LongDoubleToSInt64(long double value);
  184. extern UInt64 U64Max(void);
  185. extern UInt64 U64Add(UInt64 x, UInt64 y);
  186. extern UInt64 U64Subtract(UInt64 left, UInt64 right);
  187. extern UInt64 U64Multiply(UInt64 xparam, UInt64 yparam);
  188. extern UInt64 U64Divide(UInt64 dividend, UInt64 divisor, UInt64 *remainder);
  189. extern UInt64 U64Set(SInt32 value);
  190. extern UInt64 U64SetU(UInt32 value);
  191. extern UInt32 U32SetU(UInt64 value);
  192. extern int U64Compare(UInt64 left, UInt64 right);
  193. extern Boolean U64And(UInt64 left, UInt64 right);
  194. extern Boolean U64Or(UInt64 left, UInt64 right);
  195. extern Boolean U64Eor(UInt64 left, UInt64 right);
  196. extern Boolean U64Not(UInt64 value);
  197. extern UInt64 U64BitwiseAnd(UInt64 left, UInt64 right);
  198. extern UInt64 U64BitwiseOr(UInt64 left, UInt64 right);
  199. extern UInt64 U64BitwiseEor(UInt64 left, UInt64 right);
  200. extern UInt64 U64BitwiseNot(UInt64 value);
  201. extern UInt64 U64ShiftRight(UInt64 value, UInt32 shift);
  202. extern UInt64 U64ShiftLeft(UInt64 value, UInt32 shift);
  203. extern long double UInt64ToLongDouble(UInt64 value);
  204. extern UInt64 LongDoubleToUInt64(long double value);
  205. extern SInt64 UInt64ToSInt64(UInt64 value);
  206. extern UInt64 SInt64ToUInt64(SInt64 value);
  207. #endif
  208.  
  209. #if PRAGMA_IMPORT_SUPPORTED
  210. #pragma import off
  211. #endif
  212.  
  213. #if PRAGMA_ALIGN_SUPPORTED
  214. #pragma options align=reset
  215. #endif
  216.  
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220.  
  221. #endif /* __MATH64__ */
  222.